import { Http } from '../utils/net'
import apiCfg from '../config/api'

export function http({ withAccessToken = false } = {}) {
  const http = new Http({ baseUrl: apiCfg.baseUrl })
  if (withAccessToken === true) {
    http.interceptors.request.use((cfg) => {
      cfg.header = Object.assign(cfg.header, {
        'x-access-token': wx.getStorageSync(apiCfg.accessTokenKey),
      })
      return cfg
    })

    http.interceptors.response.use((res) => {
      if (res.statusCode < 200 || res.statusCode >= 300) {
        throw `异常响应状态码:${res.statusCode}`
      }
      return res
    })
  }
  return http
}
